package com.es.www.kyl.service.help;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.es.www.kyl.bean.res.client.QuestionDetailVO;
import com.es.www.kyl.bean.res.web.QuestionAnalysisVO;
import com.es.www.kyl.constant.BaseConstants;
import com.es.www.kyl.domain.Question;
import com.es.www.kyl.domain.QuestionAnalysis;
import com.es.www.kyl.domain.QuestionOption;
import com.es.www.kyl.mapper.QuestionAnalysisMapper;
import com.es.www.kyl.mapper.QuestionMapper;
import com.es.www.kyl.mapper.QuestionOptionMapper;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author Estranger
 * @date 2025/8/17
 * description
 */
@Service
@RequiredArgsConstructor
public class ExerciseHelper {

    private final QuestionMapper questionMapper;
    private final QuestionOptionMapper questionOptionMapper;
    private final QuestionAnalysisMapper questionAnalysisMapper;

    public QuestionDetailVO getQuestionDetail(Integer lastQuestionId) {
        QuestionDetailVO questionDetail = new QuestionDetailVO();
        //获取题干信息
        QuestionDetailVO.QuestionStemVo questionStem = new QuestionDetailVO.QuestionStemVo();
        Question question = questionMapper.selectById(lastQuestionId);
        questionStem.setQuestionId(question.getId());
        questionStem.setQuestionContent(question.getTitle());
        questionStem.setQuestionType(question.getType());
        //字符串转化为list
        if (StringUtils.isNotBlank(question.getQuestionImage())) {
            questionStem.setQuestionImageList(Arrays.asList(question.getQuestionImage().split(",")));
        }
        if (StringUtils.isNotBlank(question.getQuestionVideo())) {
            questionStem.setQuestionVideoList(Arrays.asList(question.getQuestionVideo().split(",")));
        }
        //获取选项类型
        List<QuestionOption> questionOptionList = questionOptionMapper.selectList(new LambdaQueryWrapper<QuestionOption>()
                .eq(QuestionOption::getQuestionId, lastQuestionId)
                .orderByAsc(QuestionOption::getSort)
        );
        List<QuestionDetailVO.QuestionOptionVO> optionList = questionOptionList.stream().map(questionOption -> {
            QuestionDetailVO.QuestionOptionVO option = new QuestionDetailVO.QuestionOptionVO();
            option.setOptionId(questionOption.getId());
            option.setOptionContent(questionOption.getOptionContent());
            option.setCorrectFlag(questionOption.getCorrectFlag());
            return option;
        }).collect(Collectors.toList());
        questionDetail.setQuestionOptionList(optionList);
        //获取解析类型
        List<QuestionAnalysis> questionAnalysisList = questionAnalysisMapper.selectList(new LambdaQueryWrapper<QuestionAnalysis>()
                .eq(QuestionAnalysis::getQuestionId, lastQuestionId)
                .orderByAsc(QuestionAnalysis::getSort)
        );
        List<QuestionDetailVO.QuestionAnalysisVO> analysisList = questionAnalysisList.stream().map(analysis -> {
            QuestionDetailVO.QuestionAnalysisVO questionAnalysis = new QuestionDetailVO.QuestionAnalysisVO();
            questionAnalysis.setContent(analysis.getContent());
            questionAnalysis.setType(analysis.getType());
            return questionAnalysis;
        }).collect(Collectors.toList());
        questionDetail.setQuestionStem(questionStem);
        questionDetail.setQuestionAnalysisList(analysisList);
        return questionDetail;
    }
}
